"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : HA主备高可用
Case Name   : ignore_standby_lsn_window参数正常场景验证
Create At   : 2024-4-7
Owner       : @peilinqian
Description :
    1、查询ignore_standby_lsn_window默认值
    2、gs_guc设置ignore_standby_lsn_window，不加单位
    3、gs_guc设置ignore_standby_lsn_window，加单位
    4、gs_guc设置ignore_standby_lsn_window，设置临界值
    5、alter system方式设置ignore_standby_lsn_window
    6、alter system方式设置ignore_standby_lsn_window，设置临界值
    7、修改配置文件方式设置ignore_standby_lsn_window
Expect      :
    1、查询ignore_standby_lsn_window默认值
    2、gs_guc设置ignore_standby_lsn_window，不加单位
    3、gs_guc设置ignore_standby_lsn_window，加单位
    4、gs_guc设置ignore_standby_lsn_window，设置临界值
    5、alter system方式设置ignore_standby_lsn_window
    6、alter system方式设置ignore_standby_lsn_window，设置临界值
    7、修改配置文件方式设置ignore_standby_lsn_window
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class HaPrimary(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}: start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_oguser = Node(node='PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_main(self):
        step_txt = '----step1：查询ignore_standby_lsn_window默认值；expect：默认值为0----'
        self.log.info(step_txt)
        self.init_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual('0', self.init_para, '执行失败:' + step_txt)

        step_txt = '----step2：gs_guc设置ignore_standby_lsn_window，不加单位； ' \
                   'expect：成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '120ms', '执行失败:' + step_txt)

        step_txt = '----step3：gs_guc设置ignore_standby_lsn_window，加单位； ' \
                   'expect：成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120s",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '2min', '执行失败:' + step_txt)

        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120min",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '2h', '执行失败:' + step_txt)

        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120h",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '5d', '执行失败:' + step_txt)

        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=12d",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '12d', '执行失败:' + step_txt)

        step_txt = '----step4：gs_guc设置ignore_standby_lsn_window，设置临界值； ' \
                   'expect：成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window="
                                           "2147483647", single=True)
        self.assertTrue(result, '执行失败:' + step_txt)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '2147483647ms', '执行失败:' + step_txt)

        step_txt = '----step5: alter system方式设置ignore_standby_lsn_window；' \
                   'expect： 成功----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='130s' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertIn(self.constant.alter_system_success_msg, alter_result,
                      '执行失败:' + step_txt)
        time.sleep(2)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '130s', '执行失败:' + step_txt)

        step_txt = '----step6: alter system方式设置system方式设置' \
                   'ignore_standby_lsn_window，设置临界值；expect： 成功----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window=2147483647 ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertIn(self.constant.alter_system_success_msg, alter_result,
                      '执行失败:' + step_txt)
        time.sleep(2)
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '2147483647ms', '执行失败:' + step_txt)

        step_txt = '----step7: 修改配置文件方式设置ignore_standby_lsn_window；' \
                   'expect：成功----'
        self.log.info(step_txt)
        cmd = f"echo 'ignore_standby_lsn_window=2147min' >>" \
              f"{macro.DB_INSTANCE_PATH}/postgresql.conf"
        self.log.info(cmd)
        result = self.pri_oguser.sh(cmd).result()
        self.log.info(result)
        self.pri_sh.reload_db_config()
        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '2147min', '执行失败:' + step_txt)
        cmd = f"sed -i '/ignore_standby_lsn_window=2147min/d' " \
              f"{macro.DB_INSTANCE_PATH}/postgresql.conf;" \
              f"grep 'ignore_standby_lsn_window' " \
              f"{macro.DB_INSTANCE_PATH}/postgresql.conf;"
        self.log.info(cmd)
        result = self.pri_oguser.sh(cmd).result()
        self.log.info(result)

        step_txt = '----step8：gs_guc设置ignore_standby_lsn_window，恢复默认值； ' \
                   'expect：成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window",
                                           single=True)
        self.assertTrue(result, '执行失败:' + step_txt)

        self.log.info('----查询参数----')
        self.new_para = self.common.show_param('ignore_standby_lsn_window')
        self.assertEqual(self.new_para, '0', '执行失败:' + step_txt)

    def tearDown(self):
        step_txt = '----step9: 还原参数----'
        self.log.info(step_txt)
        set_result = self.pri_sh.execute_gsguc("reload",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"ignore_standby_lsn_window="
                                               f"{self.init_para}")

        self.log.info(f'----{os.path.basename(__file__)}: end----')
        self.assertTrue(set_result, '执行失败:' + step_txt)
